package com.qk.server.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.Dto.BusinessDto;
import com.qk.common.PageResult;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.mapper.BusinessesMapper;
import com.qk.mapper.BusinessesTrackRecordMapper;
import com.qk.mapper.CustomerMapper;
import com.qk.server.BusinessesServer;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class BusinessesServerImpl extends ServiceImpl<BusinessesMapper, Business> implements BusinessesServer {
@Autowired
private BusinessesMapper businessesMapper;
@Autowired
private BusinessesTrackRecordMapper businessesTrackRecordMapper;
@Autowired
private CustomerMapper customerMapper;
    @Override
    public PageResult<Business> findBusinessByList(BusinessDto businessDto) {
        Page<Business> page =businessesMapper.selectByList(new Page<Business>(businessDto.getPage(), businessDto.getPageSize()), businessDto);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    @Override
    public Business getBusinessById(Integer id) {
        return businessesMapper.selectBusinessById(id);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateBusinessById(Business business) {
        //business表
        business.setStatus(3);//跟进中
        business.setUpdateTime(LocalDateTime.now());
        businessesMapper.updateById(business);
        //track表
        BusinessTrackRecord businessTrackRecord = new BusinessTrackRecord();
        businessTrackRecord.setBusinessId(business.getId());
        businessTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        businessTrackRecord.setTrackStatus(Integer.parseInt(business.getTrackStatus()));
        businessTrackRecord.setKeyItems(business.getKeyItems().toString());
        businessTrackRecord.setNextTime(business.getNextTime());
        businessTrackRecord.setRecord(business.getRecord());
        businessTrackRecord.setCreateTime(LocalDateTime.now());
        businessesTrackRecordMapper.insert(businessTrackRecord);
    }

    @Override
    public void toCustomer(Integer id) {
        //查询商机表
        Business business = businessesMapper.selectById(id);
        //修改商机表的商机状态
        business.setStatus(5);
        business.setUpdateTime(LocalDateTime.now());
        businessesMapper.updateById(business);
        //添加客户表
        Customer customer = new Customer();
        BeanUtils.copyProperties(business, customer);
        customer.setId( null);
        customer.setBusinessId(id);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customerMapper.insert(customer);
    }

    @Override
    public PageResult<Business> findBusinessPoolList(BusinessDto businessDto) {
        Page<Business> page = businessesMapper.selectBusinessPoolByList(new Page<Business>(businessDto.getPage(), businessDto.getPageSize()), businessDto);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }
}
